#include "BaseProjectile.h"
#include "OgreConsole.h"
#include "GameState.hpp"

using namespace Ogre;

BaseProjectile::BaseProjectile(Ogre::String name, Ogre::SceneManager* sceneMgr)
{
	m_MoveSpeed = 0.05;
	m_MaxSpeed = 400;	

	//OgreConsole::getSingleton().print("Ship created");
	m_pSceneMgr = sceneMgr;

	m_ProjectileEntity =  m_pSceneMgr->createEntity("lowpolybullet.mesh");
	m_ProjectileEntity ->setMaterialName("Bullet_UV");
	m_ProjectileNode = m_pSceneMgr->getRootSceneNode()->createChildSceneNode();

	m_ProjectileNode->attachObject(m_ProjectileEntity);
	m_ProjectileNode->setPosition(Vector3::ZERO);
	m_ProjectileNode->setScale(0.2, 0.2, 0.2);
	//Create shape.
	//m_ProjectileNode->setOrientation(pOrientation);
	//m_ProjectileNode->yaw((Degree)180);
	isActive = false;

	m_TranslateVector = Vector3::ZERO;
	m_RotateVector = Vector3::ZERO;

	aliveTimer = new Ogre::Timer();
	aliveTimerMax = 5000;
}


BaseProjectile::~BaseProjectile(void)
{
	m_ProjectileNode->removeAndDestroyAllChildren();                   //Remove All Child Nodes
	m_pSceneMgr->destroySceneNode(m_ProjectileNode->getName());           //Remove This 
}

void BaseProjectile::updateProjectile(double timeSinceLastFrame)
{
	if(!isActive)
		return;

	m_MoveScale = m_MoveSpeed  * timeSinceLastFrame;
    m_RotScale  = m_RotateSpeed * timeSinceLastFrame;
	m_ProjectileNode->translate(m_MoveScale * (m_ProjectileNode->getOrientation() * Ogre::Vector3::UNIT_Z));

	//Resets vectors
	m_TranslateVector = Vector3::ZERO;
	m_RotateVector = Vector3::ZERO;

	tickCallBack();
}

void BaseProjectile::updateTarget(Ogre::Vector3 target)
{

	//m_PlayerShip->updateTarget(target);
	m_TargetVector = target;
}


void BaseProjectile::tickCallBack()
{

}

bool BaseProjectile::getActiveState()
{
	return isActive;
}

void BaseProjectile::setActiveState(bool state)
{
	isActive = state;

	if(!isActive){
		m_ProjectileNode->setVisible(false);
	}
}

void BaseProjectile::resetProjectile(Vector3 pPos, Quaternion pOrientation){

	this->setActiveState(true);
	m_ProjectileNode->setPosition(pPos);
	m_ProjectileNode->setOrientation(pOrientation);
	m_ProjectileNode->yaw((Degree)180);
	m_ProjectileNode->setVisible(true);
	aliveTimer = new Ogre::Timer();
}